#include "XMLParser.h"

XMLParser::XMLParser(char *filename)
{
	QDomDocument dDoc( "mXML" );
	av_register_all( );

	QFile file( filename );
	if ( !file.open( IO_ReadOnly ) )
		throw("Couldn't read XML file");
	
	if ( !dDoc.setContent( &file ) )
	{
		file.close();
		throw("Couldn't set XML content");
	}
	
	file.close();
	QDomElement mRoot = dDoc.documentElement();
	if ( mRoot.tagName() != "mRoot" )
		throw("Tag wasn't mRoot!!!");
	QDomNode n = mRoot.firstChild();
	while( !n.isNull() )
	{
		QDomElement e = n.toElement();
		if( !e.isNull() )
		{
			if( e.tagName() == "scene" )
			{
				ParseScene(e);

			}
			else if(e.tagName() == "output" )
			{
				ParseOutput(e);
			}
		}
		n = n.nextSibling();
	}
	output.Process(); //beginnen eraan!
}

XMLParser::~XMLParser()
{
}

int XMLParser::ConvertTimestamp(int hours, int minutes, int secondes)
{
	while(hours > 0)
	{
		minutes = minutes + 60;
		hours--;
	}
	while(minutes > 0)
	{
		secondes = secondes + 60;
		minutes--;
	}
	return secondes;
}

void XMLParser::ParseScene(QDomElement e)
{ //scene inlezen en adden aan de output container
	std::cout << "\n----------------------------\nsrc : " << e.attribute( "src", "no file" ).toStdString();
	std::cout << std::endl << "Begin : " << e.attribute( "begin", 0 ).toStdString() << std::endl << "End: ";
	std::cout << e.attribute( "end", 0 ).toStdString()<< std::endl;
	bool videoenabled=false, audioenabled=false, delay=false;
	if(e.attribute( "videoenabled", 0 ).compare("true") == 0)
		videoenabled = true;
	if(e.attribute( "audioenabled", 0 ).compare("true") == 0)
		audioenabled = true;
	if(e.attribute( "delay", 0 ).compare("true") == 0)
		delay = true;

	Scene scene(e.attribute( "src", "no file" ).toStdString().c_str(), videoenabled,audioenabled, delay);

	QStringList list1 = e.attribute( "begin", 0 ).split(":");
	int beginseconds = ConvertTimestamp(list1.at(0).toInt(), list1.at(1).toInt(), list1.at(2).toInt());
	QStringList list2 = e.attribute( "end", 0 ).split(":");
	int endseconds = ConvertTimestamp(list2.at(0).toInt(), list2.at(1).toInt(), list2.at(2).toInt());
	if(endseconds != 0)
		scene.SetPart(beginseconds, endseconds);
	output.AddScene(scene);
}

void XMLParser::ParseOutput(QDomElement e)
{ //outputsetup inlezen & initialiseren

	std::cout << "\n---------------------------\n";
	std::cout << "name : " << e.attribute( "name", "no file" ).toStdString() << std::endl;
	std::cout << "bitrate : " << e.attribute( "bitrate", 0 ).toStdString() << std::endl;
	std::cout << "den: " << e.attribute( "den", 0 ).toStdString()<< std::endl;
	std::cout << "num : " << e.attribute( "num", 0 ).toStdString() << std::endl;
	std::cout << "gopsize : " << e.attribute( "gopsize", 0 ).toStdString() << std::endl;
	std::cout << "width : " << e.attribute( "width", 0 ).toStdString() << std::endl;
	std::cout << "height : " << e.attribute( "height", 0 ).toStdString() << std::endl;
	std::cout << "samplerate : " << e.attribute( "samplerate", 0 ).toStdString() << std::endl;
	std::cout << "audiobitrate : " << e.attribute( "audiobitrate", 0 ).toStdString() << std::endl;
	std::cout << "channels : " << e.attribute( "channels", 0 ).toStdString() << std::endl;
	std::cout << "VideoCodec : " << e.attribute( "videocodec", 0 ).toStdString() << std::endl;
	std::cout << "AudioCodec : " << e.attribute( "audiocodec", 0 ).toStdString() << std::endl;
	std::cout << "VideoEnabled : " << e.attribute( "videoenabled", 0 ).toStdString() << std::endl;
	std::cout << "AudioEnabled : " << e.attribute( "audioenabled", 0 ).toStdString() << std::endl;
	std::cout << "\n---------------------------\n";
	char buffer[40];
	int length = e.attribute( "name", "no file" ).toStdString().copy(buffer,40,0);
	buffer[length] = '\0';
	bool videoenabled=false, audioenabled=false;

	AVCodec* videoid = new AVCodec();
	videoid->id = CODEC_ID_NONE;
	AVCodec* audioid = new AVCodec();
	audioid->id = CODEC_ID_NONE;
	if(e.attribute( "videoenabled", 0 ).compare("true") == 0)
	{
		videoenabled = true;
		videoid = avcodec_find_encoder_by_name(e.attribute( "videocodec", 0 ).toStdString().c_str());
	}
	if(e.attribute( "audioenabled", 0 ).compare("true") == 0)
	{
		audioenabled = true;
		audioid = avcodec_find_encoder_by_name(e.attribute( "audiocodec", 0 ).toStdString().c_str());
	}
	if(!videoenabled && !audioenabled)
		throw("no output stream\n");

	output.EnableStreams(videoenabled, audioenabled);
	output.SetEncoder(buffer,e.attribute( "bitrate", 0 ).toInt(),e.attribute( "den", 0 ).toInt(), 
		e.attribute( "num",0 ).toInt(),e.attribute( "gopsize", 0 ).toInt(),e.attribute( "width", 0 ).toInt(),
		e.attribute( "height", 0 ).toInt(), e.attribute( "samplerate", 0 ).toInt(), 
		e.attribute( "audiobitrate", 0 ).toInt(), e.attribute( "channels", 0 ).toInt(), videoid->id, audioid->id);
}